package com.liuxinlong.jobs;

import com.liuxinlong.api.WeChatMessageInterface;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.MessageContentTypeEnum;
import com.liuxinlong.enums.MessagePushBusinessEnum;
import com.liuxinlong.modules.dao.PatentDepartmentDao;
import com.liuxinlong.modules.dao.ProjectPatentExpandDao;
import com.liuxinlong.modules.dao.WorkFlowDao;
import com.liuxinlong.modules.entity.MessageConfiguration;
import com.liuxinlong.modules.entity.MessagePushRecord;
import com.liuxinlong.modules.entity.WorkFlow;
import com.liuxinlong.modules.patent.dto.PatentProjectDto;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 风险专利监控提醒定时任务
 */
@Service
@Slf4j
@Component("checkPatentRiskTask")
public class CheckPatentRiskTask {

    @Autowired
    private WeChatMessageInterface weChatMessageInterface;

    @Autowired
    private ProjectPatentExpandDao projectPatentExpandDao;

    @Autowired
    private WorkFlowDao workFlowDao;

    @Autowired
    private PatentDepartmentDao patentDepartmentDao;

    /**
     * 业务逻辑
     * 项目专利排查结束一周未发起新品风险专利分析需求，早上9:00通知IRR登录处理
     */
    public void reptilian() {
        log.info("checkPatentRiskTask start...");
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        String currentDate = currentTime.substring(0, 10);
        String sevenDaysAgo = TimeUtils.getDaysAgo(currentDate, 7, TimeFormatConstants.YYYY_MM_DD);
        List<PatentProjectDto> patentExpandList = projectPatentExpandDao.getPendingProjectList(sevenDaysAgo, 3);
        if (ObjectUtils.isEmpty(patentExpandList)) {
            log.info("no project patent risk need to push.");
            log.info("checkPatentRiskTask end...");
            return;
        } else {
            Map<String, Map<String, String>> departmentMap = patentDepartmentDao.getDepartmentMap();
            for (PatentProjectDto projectDto : patentExpandList) {
                WorkFlow workFlow = workFlowDao.getBusinessLatestWorkflow(projectDto.getId());
                float disDays = TimeUtils.calculateTimeGapDay(workFlow.getUpdateTime(), currentTime, TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
                int days = Math.round(disDays);
                if (days > 6) {
                    Map<String, String> departmentInfo = departmentMap.get(projectDto.getDepartment());
                    if (!departmentInfo.containsKey("dutyUser")) {
                        continue;
                    }
                    List<String> targetIRRList = Arrays.asList(departmentInfo.get("dutyUser").split(","));
//                    targetIRRList.add(projectDto.getProjectManager());
                    String content = "风险管控项目 【" + projectDto.getProjectName() + "】 未发起新品风险专利分析，请登录处理！";
                    MessageConfiguration messageConfiguration = new MessageConfiguration();
                    messageConfiguration.setBusinessType(MessagePushBusinessEnum.PATENT_RISK_PROJECT.getValue());
                    messageConfiguration.setContent(content);
                    messageConfiguration.setContentType(MessageContentTypeEnum.TEXT_CARD.getValue());
                    MessagePushRecord mpr = weChatMessageInterface.pushMessage(targetIRRList, null, messageConfiguration, currentTime);
                }
            }
        }
        log.info("checkPatentRiskTask end...");
    }
}

